<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="../img/asset-favico.ico">
        <title>登录</title>
        <link rel="stylesheet" href="../css/page-health-login.css" />
        <link rel="stylesheet" href="../plugins/elementui/index.css" />
        <script src="../plugins/jquery/dist/jquery.min.js"></script>
        <script src="../plugins/healthmobile.js"></script>
        <script src="../plugins/vue/vue.js"></script>
        <script src="../plugins/vue/axios-0.18.0.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <link rel="stylesheet" href="/login/iconfont.css">
        <style>
            *{
                padding: 0;
                margin: 0;
            }
            #box{
                position: relative;
                width: 300px;
                height: 40px;
                margin: 0 auto;
                margin-top: 10px;
                background-color: #e8e8e8;
                box-shadow: 1px 1px 5px rgba(0,0,0,0.2);
            }
            .bgColor{
                position: absolute;
                left:0;
                top:0;
                width:40px;
                height: 40px;
                background-color: lightblue;
            }
            .txt{
                position: absolute;
                width: 100%;
                height: 40px;
                line-height: 40px;
                font-size: 14px;
                color: #000;
                text-align: center;
            }
            .slider{
                position: absolute;
                left:0;
                top:0;
                width: 50px;
                height: 38px;
                border: 1px solid #ccc;
                background: #fff;
                text-align: center;
                cursor: move;
            }
            .slider>i{
                position: absolute;
                top:50%;
                left:50%;
                transform: translate(-50%,-50%);
            }
            .slider.active>i{
                color:green;
            }
        </style>
    </head>
    <body data-spy="scroll" data-target="#myNavbar" data-offset="150">
    <div class="app" id="app">
        <!-- 页面头部 -->
        <div class="top-header">
            <span class="f-left"><a href="login.html"><i class="icon-back"  href="login.html"></i></a></span>
            <span class="center">传智健康</span>
            <span class="f-right"><i class="icon-more"></i></span>
        </div>
        <div style="margin-left: 20px" align="center"><h2>手机快速登录</h2></div>
        <!-- 页面内容 -->
        <div class="contentBox">
            <div class="login">
                <form id='login-form'>
                    <div class="input-row" align="center" >
                        <!--<label>手机号</label>-->
                        <div class="loginInput" >
                            手机号&nbsp;
                            <input v-model="loginInfo.telephone" id='account' type="text" style="height: 30px;width: 40%" @blur="validateTelephone" placeholder="请输入手机号" >
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <!--<input id="validateCodeButton" @click="sendValidateCode()" type="button" style="font-size: 12px" value="获取验证码">-->

                        </div>
                    </div>
                    <div class="input-row" align="center">
                        <div id="box" style="display: block" onselectstart="return false;">
                            <div class="bgColor"></div>
                            <div class="txt" >滑动验证</div>
                            <!--给i标签添加上相应字体图标的类名即可-->
                            <div class="slider"><i class="iconfont iconicon_doubleright"></i></div>
                        </div>
                        <div class="code" id="code" style="display:none">
                            <!--<label>验证码</label>-->
                            <div class="loginInput" align="center">
                                验证码&nbsp;
                                <input v-model="loginInfo.validateCode"  id='password' type="text" style="height:30px;width:40%" placeholder="请输入验证码">
                                <input id="validateCodeButton" @click="sendValidateCode()" type="button" style="font-size: 12px" value="验证码正发送中">
                            </div>
                        </div>
                    </div>
                    <div class="input-row" style="font-size: 12px" align="center">
                        <input type="checkbox" checked>
                        阅读并同意《传智健康用户协议》《传智健康隐私条款》
                    </div>
                    <div class="btn yes-btn"><a @click="login">登录</a></div>
                </form>
            </div>
        </div>
    </div>
    </body>
    <script>
        var vue = new Vue({
            el:'#app',
            data:{
                loginInfo:{}//登录信息
            },
            methods:{
                //发送验证码
                sendValidateCode(){
                    //获取用户输入的手机号
                    var telephone = this.loginInfo.telephone;
                    //校验手机号输入是否正确
                    if (!checkTelephone(this.loginInfo.telephone)) {
                        this.$message.error('请输入正确的手机号');
                        isSuccess=false;
                        slider.onmousedown = mousedownHandler;
                        return false;
                    }
                    $("#code").show();
                    $("#box").hide();
                    $("#account").attr("readOnly","true");
                    validateCodeButton = $("#validateCodeButton")[0];//使用变量validateCodeButton用来接收input标签
                    clock = window.setInterval(doLoop, 1000); //每隔一秒执行一次doLoop方法
                    axios.get("/validateCode/sendCodeForLogin.do?telephone="+telephone).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.message);
                        }else {
                            this.$message.error(res.data.message);
                        }
                    });

                },
                //登录
                login(){
                    var telephone = this.loginInfo.telephone;
                    var validateCode=this.loginInfo.validateCode;
                    //校验前端手机号码
                    if (!checkTelephone(this.loginInfo.telephone)) {
                        this.$message.error('请输入正确的手机号');
                        return;
                    }
                    if (validateCode == null || validateCode.length != 6) {
                        this.$message.error("请输入6位验证码");
                        return;
                    }
                    //如果以上校验通过，则发起请求
                    //发送请求提交注册数据
                    axios.get("/member/addMemberForLogin.do?telephone=" + telephone+"&validateCode="+validateCode).then((res) => {
                        if (res.data.flag) {
                            this.$message.success(res.data.message)
                            window.location.href="index.html";
                        } else {
                            this.$message.error(res.data.message)
                        }

                    })
                },
                //验证手机号
                validateTelephone() {
                    var telephone = this.loginInfo.telephone;
                    if (!checkTelephone(telephone)) {
                        this.$message.error('请输入正确的手机号');
                        isSuccess=false;
                        slider.onmousedown = mousedownHandler;
                        $("#code").hide();
                        $("#box").show();

                        return;
                    }
                }
            }
        });
    </script>
    <script>
        //一、定义了一个获取元素的方法
        function getEle(selector){
            return document.querySelector(selector);
        }
        //二、获取到需要用到的DOM元素
        var box = getEle("#box"),//容器
            bgColor = getEle(".bgColor"),//背景色
            txt = getEle(".txt"),//文本
            slider = getEle(".slider"),//滑块
            icon = getEle(".slider>i"),
            successMoveDistance = box.offsetWidth- slider.offsetWidth,//解锁需要滑动的距离
            downX,//用于存放鼠标按下时的位置
            isSuccess = false;//是否解锁成功的标志，默认不成功

        //三、给滑块添加鼠标按下事件
        slider.onmousedown = mousedownHandler;

        //3.1鼠标按下事件的方法实现
        function mousedownHandler(e){
            bgColor.style.transition = "";
            slider.style.transition = "";
            var e = e || window.event || e.which;
            downX = e.clientX;
            //在鼠标按下时，分别给鼠标添加移动和松开事件
            document.onmousemove = mousemoveHandler;
            document.onmouseup = mouseupHandler;
        };

        //四、定义一个获取鼠标当前需要移动多少距离的方法
        function getOffsetX(offset,min,max){
            if(offset < min){
                offset = min;
            }else if(offset > max){
                offset = max;
            }
            return offset;
        }

        //3.1.1鼠标移动事件的方法实现
        function mousemoveHandler(e){
            var e = e || window.event || e.which;
            var moveX = e.clientX;
            var offsetX = getOffsetX(moveX - downX,0,successMoveDistance);
            bgColor.style.width = offsetX + "px";
            slider.style.left = offsetX + "px";

            if(offsetX == successMoveDistance){
                success();
            }
            //如果不设置滑块滑动时会出现问题（目前还不知道为什么）
            e.preventDefault();
        };

        //3.1.2鼠标松开事件的方法实现
        function mouseupHandler(e){
            if(!isSuccess){
                bgColor.style.width = 0 + "px";
                slider.style.left = 0 + "px";
                bgColor.style.transition = "width 0.8s linear";
                slider.style.transition = "left 0.8s linear";
            }
            document.onmousemove = null;
            document.onmouseup = null;
        };

        //五、定义一个滑块解锁成功的方法
        function success(){
            isSuccess = true;
            txt.innerHTML = "验证成功";
            bgColor.style.backgroundColor ="lightgreen";
            slider.className = "slider active";
            icon.className = "iconfont icon-xuanzhong";
            //滑动成功时，移除鼠标按下事件和鼠标移动事件
            slider.onmousedown = null;
            document.onmousemove = null;
            /*$("#code").show();
            $("#box").hide();*/
            document.getElementById("validateCodeButton").click();
        };
    </script>
</html>